using System;
using System.Collections;
using UnityEngine;

public class GUILevelPresetsLoader : MonoBehaviour
{
	public RectTransform LevelPrefab;

	public Transform Canvas;

	private void Start()
	{
		DrawGameLobby();
	}

	private void Update()
	{
	}

	private void ClearCanvas()
	{
		if (!(Canvas == null))
		{
			IEnumerator enumerator = Canvas.transform.GetEnumerator();
			try
			{
				while (enumerator.MoveNext())
				{
					Transform transform = (Transform)enumerator.Current;
					UnityEngine.Object.Destroy(transform.gameObject);
				}
			}
			finally
			{
				IDisposable disposable;
				if ((disposable = (enumerator as IDisposable)) != null)
				{
					disposable.Dispose();
				}
			}
		}
	}

	public void DrawGameLobby()
	{
		if (Canvas == null || LevelPrefab == null || !(UnitZ.levelManager != null))
		{
			return;
		}
		ClearCanvas();
		for (int i = 0; i < UnitZ.levelManager.LevelPresets.Length; i++)
		{
			GameObject gameObject = UnityEngine.Object.Instantiate(LevelPrefab.gameObject, Vector3.zero, Quaternion.identity);
			gameObject.transform.SetParent(Canvas.transform);
			GUILevelPresetSelector component = gameObject.GetComponent<GUILevelPresetSelector>();
			if ((bool)component)
			{
				component.Level = UnitZ.levelManager.LevelPresets[i];
				component.Index = i;
			}
			RectTransform component2 = gameObject.GetComponent<RectTransform>();
			if ((bool)component2)
			{
				RectTransform rectTransform = component2;
				Vector2 sizeDelta = LevelPrefab.sizeDelta;
				rectTransform.anchoredPosition = new Vector2(5f, 0f - (sizeDelta.y + 5f) * (float)i);
				component2.localScale = LevelPrefab.gameObject.transform.localScale;
			}
		}
		RectTransform component3 = Canvas.gameObject.GetComponent<RectTransform>();
		RectTransform rectTransform2 = component3;
		Vector2 sizeDelta2 = component3.sizeDelta;
		float x = sizeDelta2.x;
		Vector2 sizeDelta3 = LevelPrefab.sizeDelta;
		rectTransform2.sizeDelta = new Vector2(x, (sizeDelta3.y + 5f) * (float)UnitZ.levelManager.LevelPresets.Length);
	}
}
